/********************************************************************************
* writer: vico
* create date: 2018-1-31
* description:封装二维码模块FNScanner,需要  privacy模块检查权限
* 暴露三个方法 fnStartAJPush  fnStopAJPush   fnChangeBindTags
*********************************************************************************/

(function(window){
'user strict'
var _FNScanner;
var _privacy;
var FNScanner = function(){
   this._init();
   this._fnAddEventListener();
}
FNScanner.prototype = {
  _init:function(){
    _FNScanner = api.require("FNScanner");
  },
  _initPrivacy:function(){
    _privacy = api.require("privacy");
  },
  _fnCheckIosCamera:function(pCallBack){//检查IOS是否开启摄像头，安卓不需要
    var self = this;
    if(!_privacy){
      self._initPrivacy();
    }
    _privacy.camera(function( ret, err ){
        if( ret.status ){
            console.log( JSON.stringify( ret ) );
            pCallBack()
        }else{
            api.toast({
                msg: '摄像头权限未打开',
                duration: 2000,
                location: 'bottom'
            });
        }
    });
  },
  _fnOpenView:function(pParam,pCallBack){//打开可自定义的二维码/条形码扫描器
    _FNScanner.openView({
      rect:pParam,
      fixedOn:api.frameName
    },function(ret,err){
      if(ret){
        var tEventType,tContent;
        if(ret.eventType){
          tEventType = ret.eventType;
        }
        if(ret.content){
          tContent = ret.content;
        }
        pCallBack(tEventType,tContent)//打开成功后的回调；扫码成功后的回调
      }else{
        console.log(JSON.stringify(err));//失败
      }
    })
  },
  _fnAddEventListener:function(){//添加监听事件
    api.addEventListener({
        name: 'resume'
    }, function(ret, err) {
        _FNScanner.onResume();
    });
    api.addEventListener({
        name: 'pause'
    }, function(ret, err) {
        _FNScanner.onPause();
    });
  },
  /**
   *
   * @param  {Object} pParam   二维码打开的位置和大小信息
   *  例：tParam = {
          x:0,
          y:0,
          w:300,
          h:300
      }
   * @param  {callback} pCallBack  二维码扫描打开后回调
   *    参数： tEventType,tContent
   *
   */
  fnOpenView:function(pParam,pCallBack){//打开扫描二维码，如果是IOS需要检查是否开启摄像头
    var self = this;
    if (api.systemType == 'ios') {
        self._fnCheckIosCamera(function(){
          self._fnOpenView(pParam,pCallBack)
        })
    }else{
          self._fnOpenView(pParam,pCallBack)
    }
  },
  fnCloseView:function(){//关闭二维码扫描器
      _FNScanner.closeView();
  },
  fnResumeView:function(){//恢复二维码扫描
      _FNScanner.onResume();
  },
  fnPauseView:function(){//暂停二维码扫描
      _FNScanner.onPause();
  },
  /**
   * @param  {string} pContent   生成二维码的信息
   *
   * @param  {callback} pCallBack  生成二维码后的回调
   *    参数： 生成二维码返回的图片地址
   *
   */
  fnEncodeImg:function(pContent,pCallBack){//生成二维码
      _FNScanner.encodeImg({
          content: pContent,
          saveImg: {
              path: 'fs://res/'+ pContent +'.png',
              w: 800,
              h: 800
          }
      }, function(ret, err){
          if( ret.status ){//生成二维码
              pCallBack(ret.imgPath);
          }else{//生成失败
              console.log( JSON.stringify( err ) );
          }
      });

  }
}
   window.FNScanner = FNScanner;
})(window)
